﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyAlbum.Services;
using System.Diagnostics;
using MyAlbum.Model;

namespace UnitTests
{
    /// <summary>
    /// Summary description for EntryPerformance
    /// </summary>
    [TestClass]
    public class EntryPerformance
    {
        public EntryPerformance()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            _testConfig = new TestConfig();
            entries = new List<MyAlbum.Model.Entry>();
            //first clear all the old items
            var user = new GenericUser("tal", new string[] { "Admin" });

            var oldItems = this.Service.GetItems();
            foreach (var item in oldItems)
            {
                this.Service.DeleteItem(item);
            }
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            _testConfig.Dispose();
            foreach (var item in entries)
            {
                Service.DeleteItem(item);
            }
        }
        //
        private TestConfig _testConfig;
        private List<MyAlbum.Model.Entry> entries;
        MyAlbum.Services.EntryService Service
        {
            get
            {
                return ObjectFactory.GetInstance<IEntryService>() as EntryService;
            }
        }

        private const int Count = 5000;


       

        [TestMethod]
        public void GetEntriesTest()
        {
            var item = EntryServiceTest.Create();
            item.AlbumId = new Random().Next(20);
            item.Visible = true;
            Stopwatch watch = new Stopwatch();
            List<long> getEntriesData = new List<long>();
            List<long> addItemData = new List<long>();
            List<long> updateItemData = new List<long>();
            List<long> getItemByIdData = new List<long>();
            List<long> getEntriesByAlbumData = new List<long>();
            List<long> getEntriesByTagData = new List<long>();
            List<long> getEntriesByMonthsData = new List<long>();
            List<long> getRandomEntriesData = new List<long>();

            var update = EntryServiceTest.Create();
            update.Visible = true;
            var target = ObjectFactory.GetInstance<IEntryService>();
            try
            {
                for (int i = 0; i < Count; i++)
                {
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //create new each loop
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    //check the add item method
                    watch.Start();
                    target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
                    addItemData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //check the get items method
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    watch.Start();
                    var user = new GenericUser("tal", new string[] {});

                    var entries = target.GetItems();
                    int j = entries.Count;
                    getEntriesData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //check the update method
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    update.Id = item.Id - 1;
                    watch.Start();
                    target.UpdateItem(update, new MyAlbum.Utility.GenericValidationSummary());
                    updateItemData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //check the get item by id method
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    watch.Start();
                    var t = target.GetItemById(item.Id - 1);
                    getItemByIdData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //get entries by album id
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    watch.Start();
                    var e = target.GetEntriesByAlbum(new Random().Next(20));
                    getEntriesByAlbumData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //get entries by album id
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    watch.Start();
                    var e2 = target.GetEntriesByTagName(Guid.NewGuid().ToString().Substring(0, 3));
                    getEntriesByTagData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //get entries by months
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    watch.Start();
                    var user1 = new GenericUser("tal", new string[] { "" });
                    var e3 = target.GetEntriesByMonthsSummary();
                    getEntriesByMonthsData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();

                    if (TestConfig._mode == PersistenceModes.Xml)
                    {
                        //get random entries
                        target = ObjectFactory.GetInstance<IEntryService>();
                    }
                    watch.Start();
                    var e4 = target.LoadRandomEntriesSummary(30);
                    getRandomEntriesData.Add(watch.ElapsedMilliseconds);
                    watch.Reset();
                    this.entries.Add(item);
                }
            }
            finally
            {
                Statistics.Save("GetEntries", getEntriesData);
                Statistics.Save("AddEntry", addItemData);
                Statistics.Save("UpdateEntry", updateItemData);
                Statistics.Save("GetEntryById", getItemByIdData);
                Statistics.Save("GetEntriesByAlbum", getEntriesByAlbumData);
                Statistics.Save("GetEntriesByTag", getEntriesByTagData);
                Statistics.Save("GetEntriesByMonths", getEntriesByMonthsData);
                Statistics.Save("GetRandomEntries", getRandomEntriesData);

                

                
            }

        }

    }
}
